package tree;


import tree.data.TreeNode;

/**
 *
 * 将有序数组转换为二叉搜索树
 *
 * https://leetcode.cn/problems/convert-sorted-array-to-binary-search-tree/?envType=study-plan-v2&envId=top-100-liked
 */
public class SortedArrayToBST {



    public TreeNode helper(int[] nums, int l, int r){

        if (l>r){
            return null;
        }

        int mid=(l+r)/2;

        TreeNode root=new TreeNode(nums[mid]);

        root.left=helper(nums,l,mid-1);
        root.right=helper(nums,mid+1,r);

        return root;
    }

    public TreeNode sortedArrayToBST(int[] nums) {
        return helper(nums,0,nums.length-1);
    }
}
